﻿using Entity;
using IService;
using Microsoft.AspNetCore.Mvc;

namespace XingWeiDaWMSProject.Controllers
{
    public class InboundOrderController : Controller
    {
        private readonly IInboundOrderService _inboundService;

        public InboundOrderController(IInboundOrderService inboundService)
        {
            _inboundService = inboundService;
        }


        public IActionResult Index()
        {
            return View();
        }


        [HttpPost("Add")]
        public async Task<IActionResult> Add([FromBody] InboundOrderRequest request)
        {
            try
            {
                // 1. 转换主表请求（保持不变）  
                var order = new InboundOrderEntity
                {
                    warehouse_receipt_no = request.warehouse_receipt_no,
                    warehouse_id = request.warehouse_id,
                    rule_id = request.rule_id,
                    type_id = request.type_id,
                    storage_type = request.storage_type,
                    warehouse_name = request.warehouse_name,
                    date_of_storage = DateTime.Parse(request.date_of_storage),
                    associated_order_no = request.associated_order_no,
                    suppliers_id = request.suppliers_id,
                    handler_id = request.handler_id,
                    organizations_id = request.organizations_id,
                    note = request.note
                };

                // 构造明细实体列表时，使用 InboundOrderDetailEntity2
                var details = request.details.Select(d => new InboundOrderDetailEntity2
                {
                    product_code = d.product_code,
                    curr_stk_in_num = d.inbound_quantity,
                    storage_location = d.inbound_location,
                    batch_number = d.batch_no,
                    Production_date = string.IsNullOrWhiteSpace(d.production_date)
                        ? null
                        : DateTime.Parse(d.production_date),
                    note = d.note,
                    unit_price = 0, // 若前端无单价，设为0（或从业务逻辑获取）
                    create_by = order.create_by,
                    create_time = order.create_time,
                    update_by = order.create_by,
                    update_time = order.create_time,
                    is_delete = "0"
                }).ToList();



                // 3. 调用服务层
                await _inboundService.AddInboundOrderAsync(order, details);

                return Ok(new { success = true, message = "入库单已保存（待审核）" });
            }
            catch (ArgumentException ex)
            {
                return Ok(new { success = false, message = ex.Message });
            }
            catch (Exception ex)
            {
                return Ok(new { success = false, message = $"保存失败：{ex.Message}" });
            }
        }


        [HttpGet("/api/InboundOrder/QueryPaged")]
        public async Task<IActionResult> QueryPaged([FromQuery] InboundQueryParam param)
        {
            var (data, total) = await _inboundService.QueryInboundPagedAsync(param);
            return Ok(new
            {
                code = 200,
                data,
                total,
                pageIndex = param.PageIndex,
                pageSize = param.PageSize
            });
        }


    }
}
